package com.cspg.ssss.service;

import com.alibaba.fastjson.JSONObject;
import com.cspg.ssss.domain.CheckBatchInst;
import com.cspg.ssss.domain.ProblemInst;
import com.cspg.ssss.domain.ScoreAddApplyEntity;
import com.cspg.ssss.domain.UserDetailInfoEntity;
import com.cspg.ssss.repository.ScoreAddApplyEntityMapper;
import com.cspg.ssss.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ScoreAddApplyService {
    @Autowired
    ScoreAddApplyEntityMapper scoreAddApplyEntityMapper;

    @Autowired
    GradeRoleUserService gradeRoleUserService;

    @Autowired
    WxDepartmentService wxDepartmentService;

    @Autowired
    CheckBatchService checkBatchService;

    public List<ScoreAddApplyEntity> getScoreAddApplyListByApplyUser(String uid){
        List<ScoreAddApplyEntity> retls = new ArrayList<ScoreAddApplyEntity>();
        HashMap<String,Object> usercondition = new HashMap<String, Object>();
        usercondition.put("userid",uid);
        List<UserDetailInfoEntity> userList = wxDepartmentService.getDepartmentUserList(usercondition);
        List<String> departsls = new ArrayList<String>();
        if(userList != null && userList.size() > 0){
            for(int i=0;i<userList.size();i++){
                departsls.add(userList.get(i).getDepartment_displaypath());
            }
        }
        if(departsls == null || departsls.size() <=0){
            return retls;
        }
        Map<String,Object> condition = new HashMap<String, Object>();
        condition.put("apply_depts",departsls);

        retls = scoreAddApplyEntityMapper.selectScoreAddApply(condition);
        return retls;
    }

    public List<ScoreAddApplyEntity> getScoreAddApplyListByApproveUser(String uid){
        List<ScoreAddApplyEntity> retls = new ArrayList<ScoreAddApplyEntity>();
        List<String> departs = gradeRoleUserService.getApproveDepartsByUserid(uid,1);
        if(departs == null || departs.size() <=0){
            return retls;
        }
        Map<String,Object> condition = new HashMap<String, Object>();
        condition.put("apply_depts",departs);
        condition.put("apply_status",0);
        retls = scoreAddApplyEntityMapper.selectScoreAddApply(condition);
        return retls;
    }

    public List<ScoreAddApplyEntity> getScoreAddApplyListByRecheckUser(String uid){
        List<ScoreAddApplyEntity> retls = new ArrayList<ScoreAddApplyEntity>();
        List<String> departs = gradeRoleUserService.getApproveDepartsByUserid(uid,0);
        if(departs == null || departs.size() <=0){
            return retls;
        }
        Map<String,Object> condition = new HashMap<String, Object>();
        condition.put("asse_depts",departs);
        condition.put("apply_status",1);
        retls =  scoreAddApplyEntityMapper.selectScoreAddApply(condition);
        return retls;
    }

    public int updateScoreAddApply(ScoreAddApplyEntity en){
        Map<String,Object> condition = new HashMap<String, Object>();
        condition.put("id",en.getId());
        List<ScoreAddApplyEntity> selEn = scoreAddApplyEntityMapper.selectScoreAddApply(condition);
        if(selEn != null && selEn.size() == 1){
            scoreAddApplyEntityMapper.updateScoreAddApply(en);
        }else {
            scoreAddApplyEntityMapper.insertScoreAddApply(en);
        }
        return 0;
    }

    public ScoreAddApplyEntity getScoreAddApply(String applyid){
        Map<String,Object> condition = new HashMap<String, Object>();
        condition.put("id",applyid);
        List<ScoreAddApplyEntity> selEn = scoreAddApplyEntityMapper.selectScoreAddApply(condition);
        if(selEn != null && selEn.size() == 1){
            return selEn.get(0);
        }
        return null;
    }

    public PageInfo<ScoreAddApplyEntity> getScoreAddApplyListByApplyUserPager(int pageNum, int pageSize, Map<String,Object> condition){

        PageHelper.startPage(pageNum, pageSize);
        List<ScoreAddApplyEntity> retlist = scoreAddApplyEntityMapper.selectScoreAddApply(condition);

        PageInfo<ScoreAddApplyEntity> pgInfo = new PageInfo<ScoreAddApplyEntity>(retlist);
        return pgInfo;
    }

    public PageInfo<ScoreAddApplyEntity> getScoreAddApplyListByApproverPager(int pageNum, int pageSize, Map<String,Object> condition){
        PageHelper.startPage(pageNum, pageSize);
        List<ScoreAddApplyEntity> retlist = scoreAddApplyEntityMapper.selectScoreAddApplyWithApproveUser(condition);

        PageInfo<ScoreAddApplyEntity> pgInfo = new PageInfo<ScoreAddApplyEntity>(retlist);
        return pgInfo;
    }

    public List<ScoreAddApplyEntity> getScoreAddApplyListAll(Map<String,Object> condition){
        List<ScoreAddApplyEntity> retlist = scoreAddApplyEntityMapper.selectScoreAddApply(condition);
        return retlist;
    }

    /**
     * 获取加分申请阶段
     * @param batchInst
     * @return 0-加分申请阶段 1-加分终审阶段 2-加分申请过期阶段 -1 - 未知阶段
     */
    public int getApplyStage(CheckBatchInst batchInst){
        JSONObject configObj = JSONObject.parseObject(batchInst.getConfig());
        int add_score_apply_days = configObj.getIntValue("add_score_apply");
        int add_score_review_days = configObj.getIntValue("add_score_review");
        Date today = DateUtil.getCurrentDate();
        int period = DateUtil.workDaysPeriod(batchInst.getStart_date(),today);

        if(period <= add_score_apply_days){
            return 0;
        }

        if(period > add_score_apply_days && period < (add_score_apply_days + add_score_review_days)){
            return 1;
        }
        if(period > (add_score_apply_days + add_score_review_days)){
            return 2;
        }
        return -1;
    }

}
